package com.atguigu.spzx.user.controller;

import com.atguigu.spzx.model.dto.user.LoginDto;
import com.atguigu.spzx.model.dto.user.UserRegisterDto;
import com.atguigu.spzx.model.pojo.h5.UserInfo;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.h5.UserInfoVo;
import com.atguigu.spzx.user.service.UserInfoService;
import com.atguigu.spzx.utils.DYLAssert;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user/userInfo")
@Tag(name = "用户信息管理")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterDto userRegisterDto) {
        DYLAssert.isTrue(userInfoService.doRegister(userRegisterDto), "注册失败");
        return Result.ok();
    }

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result<String> login(@RequestBody LoginDto loginRequest) {
        return Result.ok(userInfoService.doLogin(loginRequest));
    }

    @GetMapping("/auth/toAuthUser/{token}")
    @Operation(summary = "获取用户信息，用于鉴权")
    public UserInfo getUserByToken(@PathVariable("token") String token) {
        return userInfoService.getUserByToken(token);
    }

    @GetMapping("/auth/getCurrentUserInfo")
    @Operation(summary = "获取当前用户的用户信息")
    public Result<UserInfoVo> getCurrentUserInfo() {
        return Result.ok(userInfoService.getCurrentUserInfo());
    }
}
